<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/common.css">
</head>
<body>
	<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
		<form class="form-horizontal" onsubmit="return false" id="form">
			<fieldset>
				<div class="form-group">
					<div id="filediv" class="col-md-10"></div>
				</div>
				<button type="button" class="layui-btn" id="test1">
					<i class="layui-icon">&#xe67c;</i>点击选择文件上传
				</button>

				<div class="form-actions">
					<div class="row" align="center">
						<div class="col-md-12">
							<button class="btn btn-primary" type="submit" onclick="add(1)">
								<i class="fa fa-save"></i> 保存
							</button>
							<button class="btn btn-primary" type="submit" onclick="add(2)">
								<i class="fa fa-save"></i> 保存并提交审核
							</button>
						</div>
					</div>
				</div>

			</fieldset>
		</form>
	</div>
	<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../js/common.js"></script>
	<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript" src="../../js/dict.js"></script>
	<script type="text/javascript">
		var zfiles = [];
		layui.use([ 'layer' ], function() {
			var layer = layui.layer;
			
		});

		$('#form').bootstrapValidator();

		function add(fileStatus) {
			var bootstrapValidator = $("#form").data('bootstrapValidator');
			bootstrapValidator.validate();
			if (!bootstrapValidator.isValid()) {
				return;
			}

			var formdata = $("#form").serializeObject();
			for(i = 0; i < zfiles.length; i++) {
				zfiles[i].status = fileStatus;
			}
			formdata.datas = zfiles;
			$.ajax({
				type : 'post',
				url : urlRootPrefix + '/hdContents',
				contentType : "application/json; charset=utf-8",
				data : JSON.stringify(formdata),
				success : function(data) {
					if (!data.success) {
						layer.msg(data.msg, {
							shift : -1,
							time : 3000
						}, function() {
						});
						$("button[type='submit']").removeAttr("disabled");
						return;
					}
					layer.msg("添加成功", {
						shift : -1,
						time : 1000
					}, function() {
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						parent.example.ajax.reload();
						parent.layer.close(index); //再执行关闭
					});
				}
			});
		}
		layui.use('upload', function(){
			var upload = layui.upload;
			upload.render({
			    elem: '#test1' //绑定元素
			    ,accept: 'file' //允许上传的文件类型
			    ,url: urlRootPrefix + '/files' //上传接口
			    ,size: 20480 //最大允许上传的文件大小
			    ,done: function(data, index, upload){
			    	if(!data.success){
						layer.msg(data.msg, {shift: -1, time: 3000}, function(){});
						$("button[type='submit']").removeAttr("disabled");
						return;
					}
			    	layer.msg("上传成功");
				 	//console.log(data);
				 	var data = data.data;
				 	var url = data.url;
            		var type = data.type;
            		var u = "";
            		//var src = urlRootPrefix + "/statics" + url;
            		var src = urlRootPrefix +"/statics" + url;
            		if(zfiles==null){
            			zfiles = new Array();
            		}
            		//var src = data.downUrl;
            		if(type == 1) {
            			u = "<div fdkey='"+zfiles.length+"'>"
            				+"<a target='_blank' href='" + src +"' ><img width='300' src='" + src + "'></img></a>"
            				+"<a href='javascript:void(0);' style='padding-left:30px;' onclick='rmFile(this)'>删除</a>"
            				+"</div>";
            		} else {
            			u = "<div fdkey='"+zfiles.length+"'>"
        				+"<a target='_blank' href='" + src +"' style='display:block;float:left;width:300px;'>点击查看'"+url.split(".")[1]+"'文件</a>"
        				+"<a href='javascript:void(0);' style='padding-left:30px;' onclick='rmFile(this)'>删除</a>"
        				+"</div>";
            		}
			    	$("#filediv").append(u+"<br/>");
			    	zfiles.push(data);
			    	//console.log(zfiles);
			    }
			  });
		});
		function rmFile(obj) {
			var k = $(obj).parent().attr("fdkey");
			zfiles.splice(k, 1);
			$(obj).parent().remove();
			//console.log(zfiles);
		}
	</script>
</body>
</html>